<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Loading</title>
<style type="text/css">
body {
	width: 1920px;
	height: 1080px;
	padding: 0;
	margin: 0;
	background: url(background.jpg) no-repeat;
}	
.clogo {
	text-align: center;
	margin-top: 360px;
	margin-left:660px;
	width: 600px;
	font-size: 24px;
	color: white;
}

.msg {
}

</style>
</head>
<body>
	<div class="clogo">
	       <div class="msg" id="msg">  ... </div>
		<br/>
		<br/>
		<img src="loading.gif"/>
		<br/>
		<br/>
		<span id="updatelable"> ...</span>
	</div>
</body>
</html>


<script type="text/javascript">
var pakobj = null;
var timeout_sec = 20;
var max_idletime = 120;
var portalurl = "";
var msgid = document.getElementById("msg");
var updatelable = document.getElementById("updatelable");

var updating_str = "Updating";

function reset_lang_viet() {
	try {
		var lid = localStorage.getItem("tva_lang_id");
		if ( !!lid && lid.indexOf("ta") >= 0 ) {
			msgid.innerHTML = "Đang tải ...";
			updatelable.innerHTML = "Đang tải ...";
			updating_str = "Đang tải";
		} else {
			msgid.innerHTML = "Loading ...";
			updatelable.innerHTML = "Updating ...";
		}
	}catch(e){}
}
reset_lang_viet();



function uni_onready(){
	var uiSettings = wae.create("UI.UISettings");
	uiSettings.set1080P();

//    localStorage.setItem("cache_server","23.237.112.154");

    var first_startup = localStorage.getItem("update_apk_first_run");
    if ( ! first_startup ) {
     	localStorage.setItem("update_apk_first_run", "no" );
		localStorage.setItem("launcher_fullmode", "yes");
	}

 	pakobj = wae.create("WAE.AppPackage");  
 	setTimeout( check_update, 1000 * 3 );

	var SERVER_URL = waeparams.getContentProviderPersistence("mainhosturl");

	var portal_id = parseInt(localStorage.getItem("Portal_ID"));
	if ( isNaN( portal_id ) ) {
		portal_id = 46;
	}
	if ( portal_id != 46 ) {
		portal_id = 46;
	}

//	portalurl = SERVER_URL + "download/ZIP_CONFIG_29.xml#portal_29/html/mainplus.html";

	portalurl = SERVER_URL + "download/ZIP_CONFIG_"+ portal_id + ".xml#portal_" + portal_id+ "/html/mainplus_max.html";
	portalurl = portalurl.replace("http://", "tva://" );
	portalurl = portalurl.replace("https://", "tvas://" );

	//console.log("load url : " + portalurl );
	location.replace(  portalurl ) ;

}

function check_update () {
 	if ( pakobj == null ) {
 		alert( "Error: fail to get app package info!" );
 		return;
 	}
	
 	if ( pakobj.getIdleTime() > 1000 * timeout_sec ) {
 		reload_page();
 	}

 	if ( !! pakobj.getUpdateError() ) {
		console.log("Error: " + pakobj.getUpdateError() );
 		reload_page();
 	}
	
	updatelable.innerHTML = updating_str + " [ " + pakobj.getUpdateRate() + "%] ... ";

	setTimeout( check_update, 1000 * 3 );
}


var startup_time = sessionStorage.getItem("loading_startup_time");
if ( !! startup_time ) 
	startup_time = parseInt( startup_time );

if ( ! startup_time || isNaN(startup_time) ) {
	var myDate = new Date();
	startup_time = myDate.getTime();
	sessionStorage.setItem("loading_startup_time", startup_time );
} 

function get_idletime() {
	var check_time = (new Date()).getTime();
	var idletime = check_time - startup_time;
	return idletime;
}

var c = Math.floor(get_idletime() / 1000 );
var msgDiv = document.getElementById("msg");
var msgText = msgDiv.innerHTML;
for( var i=0; i<c; i++ ) {
	msgText += ".";
}
msgDiv.innerHTML = msgText;

function reload_page() {
	var idletime = get_idletime() / 1000 ;
	if ( idletime > max_idletime ) {
//		alert("網路連接失敗，確認進入設定！");
		alert("Connect to Server Failed!\n\nClick OK to setup network!");

		console.log("start activty..");
		var intent = wae.create("Intent");
		intent.startActivity("", "com.vatata.wae.localapp.LocalAppActivity");

//	  intent.startActivity("com.android.tv.settings", "com.android.tv.settings.connectivity.NetworkActivity");  

		setTimeout( function(){
			  var app = wae.create("Application");
			  app.finishActivity();
		}, 1000 * 3 );
		return;
	}
	
	setTimeout( function(){
		console.log("load url : " + portalurl );
		location.replace(  portalurl ) ;
	}, 1000 * 1 );
}

window.onWAEReady = uni_onready;
</script>

